﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Data;
using System.Reflection;
using System.Web;
using System.Web.Configuration;

public static class Utils
{
    public static string GetApplicationSetting(string appSettingKey)
    {
        var settings = (NameValueCollection)WebConfigurationManager.GetSection(Constants.WEB_CONFIGURATION_MANAGER_SECTION);
        string result = settings[appSettingKey];
        if (string.IsNullOrEmpty(result))
        {
            var ex = new ArgumentException("No value found in '/configuration/DGPCustom/Settings' for '" + appSettingKey + "'");
            throw ex;
        }
        return result;
    }
    private static bool CanAddAsDataColumn(Type type)
    {
        return type == typeof(decimal) || type == typeof(decimal?) ||
            type == typeof(int) || type == typeof(int?) ||
            type == typeof(byte) || type == typeof(byte?) ||
            type == typeof(bool) || type == typeof(bool?) ||
            type == typeof(DateTime) || type == typeof(DateTime?) ||
            type == typeof(string);
    }
    private static Type GetDataCellType(Type type)
    {
        if (type == typeof(decimal?))
        {
            return typeof(decimal);
        }
        if (type == typeof(byte?))
        {
            return typeof(byte);
        }
        if (type == typeof(int?))
        {
            return typeof(int);
        }
        if (type == typeof(bool?))
        {
            return typeof(bool);
        }
        if (type == typeof(DateTime?))
        {
            return typeof(DateTime);
        }
        return type;
    }
    private static object GetDataCellValue(PropertyInfo Field, object Target)
    {
        var type = Field.PropertyType;
        var value = Field.GetValue(Target, null);
        if (type == typeof(decimal) ||
            type == typeof(int) ||
            type == typeof(byte) ||
            type == typeof(bool) ||
            type == typeof(DateTime) ||
            type == typeof(string))
        {
            return value;
        }
        if (type == typeof(decimal?))
        {
            if (((decimal?)value).HasValue) return ((decimal?)value).Value;
        }
        if (type == typeof(byte?))
        {
            return ((byte?)value).HasValue ? (object)((byte?)value).Value : DBNull.Value;
        }
        if (type == typeof(int?))
        {
            return ((int?)value).HasValue ? (object)((int?)value).Value : DBNull.Value;
        }
        if (type == typeof(bool?))
        {
            if (((bool?)value).HasValue) return ((bool?)value).Value;
        }
        if (type == typeof(DateTime?))
        {
            return ((DateTime?)value).HasValue ? (object)((DateTime?)value).Value : DBNull.Value;
        }
        return DBNull.Value;
    }
    public static DataTable ToDataTable<T>(this IEnumerable<T> source, string PKColumn)
    {
        var result = new DataTable();
        var type = typeof(T);
        var fields = type.GetProperties();
        foreach (var field in fields)
        {
            if (CanAddAsDataColumn(field.PropertyType))
            {
                result.Columns.Add(field.Name, GetDataCellType(field.PropertyType));
            }
        }
        if (string.IsNullOrEmpty(PKColumn) == false && result.Columns.Contains(PKColumn))
        {
            result.PrimaryKey = new[] { result.Columns[PKColumn] };
        }
        foreach (T item in source)
        {
            var row = result.NewRow();
            foreach (var field in fields)
            {
                if (CanAddAsDataColumn(field.PropertyType))
                {
                    row[field.Name] = GetDataCellValue(field, item);
                }
            }
            result.Rows.Add(row);
        }
        return result;
    }
    public static DataTable ToDataTable<T>(this IEnumerable<T> source)
    {
        return ToDataTable(source, null);
    }
    public static string Truncate(this object source, int startIndex, int length)
    {
        string sub = source.ToString().Substring(0, length);
        if (sub.EndsWith(" ") || sub.EndsWith(".")) return sub + "...";

        int space = sub.LastIndexOf(' ');
        int point = sub.LastIndexOf('.');
        return space > point ? sub.Substring(0, space) + "..." : sub.Substring(0, point) + "..";
    }
    public static string BuildURL(PageType page, int ID, string title)
    {
        var title2 = title.Replace(" ", "-").Replace("?", "").Replace("!", "").Replace(",", "").Replace(".", "");
        switch (page)
        {
            case PageType.Categorii:
                return "Category.aspx?ID=" + ID;
            //return HttpUtility.UrlEncode("Categorie-" + ID + @"-" + title2);
            case PageType.Articole:
                return "Article.aspx?ID=" + ID;
            //return HttpUtility.UrlEncode("Articol-" + ID + @"-" + title2);
            default:
                return string.Empty;
        }
    }
}
public enum PageType
{
    Categorii,
    Articole
}
